package com.situ.blog.service.impl;

import com.situ.blog.dao.IBlogDao;
import com.situ.blog.dao.impl.BlogDaoImpl;
import com.situ.blog.listener.BlogExcelListener;
import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.service.IBlogService;
import com.situ.blog.util.ExcelUtil;
import com.situ.blog.util.PageRusult;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class BlogSeviceImpl implements IBlogService {
    IBlogDao blogDao = new BlogDaoImpl();
    @Override
    public PageRusult<Blog> selectByPage(BlogQuery blogQuery) {
        int totalCount = blogDao.totalCount(blogQuery);
        //int totaPage = (int)Math.ceil((double)totalCount/blogQuery.getLimit());
        List<Blog> list = blogDao.selectByPage(blogQuery);
        PageRusult<Blog> pageResult = new PageRusult<>(0,"",totalCount,list);
        System.out.println(pageResult);
        return pageResult;
    }

    @Override
    public void add(Blog blog) {
        blogDao.add(blog);
    }

    @Override
    public int deleteById(int id) {
        int count= blogDao.deleteById(id);
         return count;
    }

    @Override
    public Blog selectById(String id) {
        Blog blog = blogDao.selectById(id);
        return blog;
    }

    @Override
    public void updateStatus(String id,int status) {
        blogDao.updateStatus(id,status);
    }

    @Override
    public void update(Blog blog) {
        blogDao.update(blog);
    }

    @Override
    public void deleteAll(String[] ids) {
        //遍历，然后调用deleteById
        for (String id : ids) {
            blogDao.deleteById(Integer.parseInt(id));
        }
    }

    @Override
    public List<Blog> selectAll() {
        return blogDao.selectAll();
    }

    @Override
    public void importExcel(HttpServletRequest req) {
        ExcelUtil.importExcel(req, new BlogExcelListener(), Blog.class);
    }
}
